﻿using AntdUI;
using AntdUI.In;
using BST250301TestStand.Models;
using BST250301TestStand.Models.Helpers;
using BST250301TestStand.Models.Utils;
using HslCommunication.Profinet.LSIS;
using MySql.Data.MySqlClient;
using Mysqlx.Crud;
using NPOI.SS.Formula.Functions;
using NPOI.Util;
using OpenTK.Audio.OpenAL;
using ScottPlot;
using ScottPlot.Colormaps;
using ScottPlot.Plottables;
using ScottPlot.WinForms;
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;
using static BST250301TestStand.Models.Utils.PLCConfig;
using static BST250301TestStand.Models.Utils.TCUConfig;
using static System.Net.Mime.MediaTypeNames;

namespace BST250301.Views
{
    public partial class ShowReal : UserControl
    {
        public bool submit;
        public static bool IsTestting;
        private ProjectItem _item;
        private int _seconds = 0;
        private List<ShowDataRecord> _ShowList = new List<ShowDataRecord>();
        private readonly int source_select;
        readonly ScottPlot.Plottables.Crosshair CH;
        static ScottPlot.Plottables.SignalXY MySignal;
        ScottPlot.Plottables.Crosshair MyCrosshair;
        public ShowReal(ProjectItem item, List<ShowDataRecord> listdatashow, bool autoPhoto, int seconds = 0)
        {
            this._item = item;
            this._seconds = seconds;
            this._ShowList = listdatashow;

            InitializeComponent();


            formsPlot1.Plot.Title("PLC数据");
            formsPlot1.Plot.XLabel("Time(s)");

            formsPlot2.Plot.Title("TCU数据");
            formsPlot2.Plot.XLabel("Time(s)");

            formsPlot1.Plot.Font.Automatic();
            formsPlot2.Plot.Font.Automatic();
            Init();           

            Task.Delay(50);
            if (autoPhoto)
            {
                TakePhoto(false);
                Task.Delay(50);
                submit = true;
                Thread.Sleep(10);
                this.Dispose();
            }
        }
        private void Init()
        {
            for (int i = 0; i < this._ShowList.Count; i++)
            {
                var item = this._ShowList[i];
                if (item.From == "TCU")
                {
                    double[] ms = item.ListX.ToArray();
                    double[] dataY = item.ListY.ToArray();
                    double[] dataC = item.ListC.ToArray();


                    if (item.AppName.Contains("Pressure"))
                    {
                        //var db = dataY.Select(n => n * 100).ToArray();
                        var sigY = formsPlot2.Plot.Add.SignalXY(ms, dataY);
                        var sigC = formsPlot2.Plot.Add.SignalXY(ms, dataC);

                        var yAxis2 = formsPlot2.Plot.Axes.AddLeftAxis();
                        sigY.Axes.XAxis = formsPlot2.Plot.Axes.Bottom; // standard X axis
                        sigY.Axes.YAxis = yAxis2; // custom Y axis
                        yAxis2.LabelText = $"Secondary Y Axis:{item.Unit}";

                        int hex = (0x00F0F0 + i * 0x0A);
                        sigY.Color = ScottPlot.Color.FromHex(hex.ToString("X"));
                        sigY.LegendText = $"{item.Title}/{item.Unit} 反馈";
                        sigC.LegendText = $"{item.Title}/{item.Unit} 控制";
                    }
                    else
                    {
                        var sigY = formsPlot2.Plot.Add.SignalXY(ms, dataY);
                        var sigC = formsPlot2.Plot.Add.SignalXY(ms, dataC);

                        int hex = (0x00F0F0 + i * 0x0A);
                        sigY.Color = ScottPlot.Color.FromHex(hex.ToString("X"));
                        sigY.LegendText = $"{item.Title}/{item.Unit} 反馈";
                        sigC.LegendText = $"{item.Title}/{item.Unit} 控制";
                    }
                    formsPlot2.Plot.YLabel($"{item.Unit}");
                    formsPlot2.Plot.ShowLegend();
                }
                if (item.From == "PLC")
                {
                    double[] ms = item.ListX.ToArray();
                    double[] dataY = item.ListY.ToArray();
                    double[] dataC = item.ListC.ToArray();

                    var sigY = formsPlot1.Plot.Add.SignalXY(ms, dataY);
                    var sigC = formsPlot1.Plot.Add.SignalXY(ms, dataC);
                    int hex = (0x00F0F0 + i * 0x0A);
                    sigY.Color = ScottPlot.Color.FromHex(hex.ToString("X"));
                    sigY.LegendText = $"{item.Title}/{item.Unit} 反馈";
                    sigC.LegendText = $"{item.Title}/{item.Unit} 控制";

                    formsPlot1.Plot.YLabel($"{item.Unit}");
                    formsPlot1.Plot.ShowLegend();
                }
            }
            formsPlot1.Plot.Font.Automatic();
            formsPlot2.Plot.Font.Automatic();
            formsPlot1.Refresh();
            formsPlot2.Refresh();

            return;
        }


        private void buttonAdd_Click(object sender, EventArgs e)
        {
            TakePhoto(true);
        }
        private async Task TakePhoto(bool isopen)
        {
            var dt = DateTime.Now;
            var path = $"D:\\Record\\{dt.ToString("yyyy")}\\{dt.ToString("MM")}\\{dt.ToString("dd")}\\Images\\";
            if (CSV.CreatePath(path))
            {
                formsPlot1.Plot.SavePng($"{path}PLC_SN_{this._item.SN}_{dt.ToString("HH_mm_ss")}.png", 1200, 800);
                formsPlot2.Plot.SavePng($"{path}TCU_SN_{this._item.SN}_{dt.ToString("HH_mm_ss")}.png", 1200, 800);
                if (isopen)
                {
                    Process.Start(new ProcessStartInfo($"{path}TCU_SN_{this._item.SN}_{dt.ToString("HH_mm_ss")}.png") { UseShellExecute = true });
                    Process.Start(new ProcessStartInfo($"{path}PLC_SN_{this._item.SN}_{dt.ToString("HH_mm_ss")}.png") { UseShellExecute = true });
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            submit = true;
            Thread.Sleep(10);
            this.Dispose();
        }
    }
}
